package com.internetCafes.spms.web.sys.controller;

import com.internetCafes.spms.common.CommonConstants;
import com.internetCafes.spms.common.utils.DateUtils;
import com.internetCafes.spms.common.utils.ShiroUtils;
import com.internetCafes.spms.core.model.pageModel.DataGrid;
import com.internetCafes.spms.core.utils.Query;
import com.internetCafes.spms.core.utils.R;
import com.internetCafes.spms.web.sys.model.ConfInfo;
import com.internetCafes.spms.web.sys.model.EducationArticleInfo;
import com.internetCafes.spms.web.sys.vo.EducationArticleInfoVo;
import com.internetCafes.spms.web.sys.service.ConfInfoService;
import com.internetCafes.spms.web.sys.service.EducationArticleInfoService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author LinYe
 * @date 2020/10/29
 */
@RestController
@RequestMapping("/sys/education")
public class EducationArticleInfoController {

    @Autowired
    EducationArticleInfoService educationArticleInfoService;

    @Autowired
    ConfInfoService confInfoService;

    @GetMapping("/list")
    @RequiresPermissions("sys:education:list")
    public R list(@RequestParam Map<String, Object> params, HttpServletRequest request){
        Query query = new Query(params);
        params = new HashMap<>();

        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String pIdType = request.getParameter("pIdType");
        String idType = request.getParameter("idType");
        String createTime = request.getParameter("createTime");
        String createBy = request.getParameter("createBy");
        String sort = request.getParameter("sort");
        String visible = request.getParameter("visible");

        if (StringUtils.isNotBlank(title)) {
            params.put("title",title);
        }

        if (StringUtils.isNotBlank(createTime)) {
            params.put("content", content);
        }

        if (StringUtils.isNotBlank(pIdType)) {
            params.put("pIdType", pIdType);
        }

        if (StringUtils.isNotBlank(idType)) {
            params.put("idType", Long.parseLong(idType));
        }

        if (StringUtils.isNotBlank(createTime)) {
            params.put("createTime", DateUtils.strToDate(createTime));
        }

        if (StringUtils.isNotBlank(createBy)) {
            params.put("createBy", Long.parseLong(createBy));
        }

        if (StringUtils.isNotBlank(sort)) {
            params.put("sort", Long.parseLong(sort));
        }

        if (StringUtils.isNotBlank(visible)) {
            params.put("visible", Integer.parseInt(visible));
        }
        DataGrid dataGrid = educationArticleInfoService.dataGrid(params, query.getPage(), query.getLimit());
        List<EducationArticleInfo> list = (List<EducationArticleInfo>) dataGrid.getList();
        for (int i = 0; i < list.size(); i++) {
            EducationArticleInfo educationArticleInfo = list.get(i);
            ConfInfo confInfo = confInfoService.findById(educationArticleInfo.getIdType());
            EducationArticleInfoVo result=new EducationArticleInfoVo();
            BeanUtils.copyProperties(educationArticleInfo,result);
            result.setIdTypeName(confInfo.getName());
            list.set(i,result);
        }
        dataGrid.setList(list);
        params = new HashMap<>();
        List<ConfInfo> confInfos;
        if (pIdType.equals("")){
            pIdType="conf_certificate_type";
            params.put("type", pIdType);
            params.put("status", CommonConstants.CommonStatus.ENABLE);
            confInfos =new ArrayList<>();
            List<ConfInfo> pIdTypeList = confInfoService.query(params);
            for (ConfInfo confInfo:pIdTypeList) {
                params.replace("type", confInfo.getCode());
                List<ConfInfo> idTypeList = confInfoService.query(params);
                confInfos.addAll(idTypeList);
            }
        }else {
            params.put("type", pIdType);
            params.put("status", CommonConstants.CommonStatus.ENABLE);
            confInfos = confInfoService.query(params);
        }
        return R.ok().put("data", dataGrid).put("confInfos",confInfos);
    }

    @PostMapping("/save")
    @RequiresPermissions("sys:education:save")
    public R save(EducationArticleInfo educationArticleInfo){
        educationArticleInfo.setCreateBy(ShiroUtils.getUserId());
        educationArticleInfo.setCreateTime(new Date());
        educationArticleInfoService.saveEducationArticle(educationArticleInfo);
        return R.ok();
    }

    @GetMapping("/delete/{id}")
    @RequiresPermissions("sys:education:delete")
    public R delete(@PathVariable("id")Long id){
        educationArticleInfoService.deleteEducationArticle(id);
        return R.ok();
    }

    @PostMapping("/update")
    @RequiresPermissions("sys:education:update")
    public R update(EducationArticleInfo educationArticleInfo){
        educationArticleInfoService.updateEducationArticle(educationArticleInfo);
        return R.ok();
    }

    @GetMapping("/getDetail")
    @RequiresPermissions("sys:education:info")
    public R getDetail(@RequestParam Map<String,Object> params){
        List<ConfInfo> confInfos = confInfoService.query(params);
        params=new HashMap<>();
        params.put("pIdType",confInfos.get(0).getType());
        params.put("idType",confInfos.get(0).getId());
        List<EducationArticleInfo> educationArticleDetail = educationArticleInfoService.getEducationArticleDetail(params);
        R ok = R.ok();
        ok.put("data",educationArticleDetail);
        return ok;
    }
}
